home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_std / array2.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  6.7 KB  |  397 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class ARRAY2[G]
  5.    
  6. inherit 
  7.    ANY 
  8.       redefine copy, is_equal 
  9.       end;
  10.    
  11. creation {ANY}
  12.    make, array2
  13.  
  14. feature {ARRAY2}
  15.    
  16.    storage: ARRAY[ARRAY[G]];
  17.    
  18. feature {ANY}
  19.    
  20.    make(l1, u1, l2, u2: INTEGER) is
  21.       require
  22.      l1 <= u1;
  23.      l2 <= u2;
  24.       local
  25.      i: INTEGER;
  26.      sa: ARRAY[G];
  27.       do
  28.      from  
  29.         !!storage.make(l1,u1);
  30.         i := storage.lower;
  31.      until
  32.         i > storage.upper
  33.      loop
  34.         !!sa.make(l2,u2);
  35.         storage.put(sa,i);
  36.         i := i + 1;
  37.      end;
  38.       ensure
  39.      lower1 = l1;
  40.      upper1 = u1;
  41.      lower2 = l2;
  42.      upper2 = u2;
  43.       end;
  44.    
  45.    array2(s: ARRAY[ARRAY[G]]) is
  46.       require
  47.      s /= Void;
  48.      s.count > 0;
  49.       local
  50.      i, l2, u2: INTEGER;
  51.       do
  52.      storage := s;
  53.      debug
  54.         from  
  55.            i := storage.lower + 1;
  56.         until
  57.            i > storage.upper
  58.         loop
  59.            if lower2 /= storage.item(i).lower then
  60.           std_error.put_string("ARRAY2: Bad `lower2' at line ")
  61.           std_error.put_integer(i);
  62.           std_error.put_new_line;
  63.           crash;
  64.            elseif upper2 /= storage.item(i).upper then
  65.           std_error.put_string("ARRAY2: Bad `upper2' at line ")
  66.           std_error.put_integer(i);
  67.           std_error.put_new_line;
  68.           crash;
  69.            end;
  70.            i := i + 1;
  71.         end;
  72.      end;
  73.       ensure
  74.      storage = s;
  75.       end;
  76.    
  77. feature {ANY}
  78.    
  79.    lower1: INTEGER is
  80.       do
  81.      Result := storage.lower;
  82.       end;
  83.    
  84.    upper1: INTEGER is
  85.       do
  86.      Result := storage.upper;
  87.       end;
  88.    
  89.    count1: INTEGER is
  90.       do
  91.      Result := storage.count;
  92.       end;
  93.    
  94.    lower2: INTEGER is
  95.       do
  96.      Result := storage.first.lower;
  97.       end;
  98.    
  99.    upper2: INTEGER is
  100.       do
  101.      Result := storage.first.upper;
  102.       end;
  103.    
  104.    count2: INTEGER is
  105.       do
  106.      Result := storage.first.count;
  107.       end;
  108.    
  109.    count: INTEGER is
  110.       do
  111.      Result := count1 * count2;
  112.       end;
  113.    
  114.    valid_index(i, j: INTEGER): BOOLEAN is
  115.       do
  116.      Result := (lower1 <= i and then
  117.             i <= upper1 and then
  118.             lower2 <= j and then
  119.             j <= upper2); 
  120.       end;
  121.       
  122.    item(i, j: INTEGER): G is
  123.       require
  124.      valid_index(i,j);
  125.       do
  126.      Result := storage.item(i).item(j);
  127.       end;
  128.    
  129.    is_equal(other: like Current): BOOLEAN is
  130.       local
  131.      i: INTEGER;
  132.       do
  133.      if Current = other then
  134.         Result := true;
  135.      elseif lower1 /= other.lower1 then
  136.      elseif upper1 /= other.upper1 then
  137.      elseif lower2 /= other.lower2 then
  138.      elseif upper2 /= other.upper2 then
  139.      else
  140.         from
  141.            Result := true;
  142.            i := storage.lower;
  143.         until
  144.            not Result or else i > storage.upper
  145.         loop
  146.            Result := storage.item(i).is_equal(other.storage.item(i));
  147.            i := i + 1;
  148.         end;
  149.      end;
  150.       end;
  151.    
  152. feature {ANY}
  153.    
  154.    put(x: G; i, j: INTEGER) is
  155.       require
  156.      valid_index(i,j);
  157.       do
  158.      storage.item(i).put(x,j);     
  159.       end;
  160.    
  161.    swap(i1, j1, i2, j2: INTEGER) is
  162.       require
  163.      valid_index (i1,j1);
  164.      valid_index (i2,j2);
  165.       local
  166.      tmp: G;
  167.       do
  168.      tmp := item(i1,j1);
  169.      put(item(i2,j2),i1,j1);
  170.      put(tmp,i2,j2);
  171.       end;
  172.  
  173.    copy(other: like Current) is
  174.       local
  175.      i: INTEGER;
  176.       do
  177.      make(other.lower1,other.upper1,other.lower2,other.upper2);
  178.      from  
  179.         i := other.lower1;
  180.         i := storage.lower;
  181.      until
  182.         i > storage.upper
  183.      loop
  184.         storage.item(i).copy(other.storage.item(i));
  185.         i := i + 1;
  186.      end;
  187.       end;
  188.    
  189. feature 
  190.  
  191.    set_all_with(x: G) is
  192.       local 
  193.          i: INTEGER;
  194.       do
  195.      from 
  196.         i := storage.lower
  197.      until
  198.         i > storage.upper
  199.          loop
  200.             storage.item(i).set_all_with(x)
  201.         i := i + 1
  202.          end;
  203.       end;
  204.  
  205.    nb_occurrences(elt: G): INTEGER is
  206.      -- Number of occurrences using `equal'.
  207.      -- See also `fast_nb_occurrences' to chose
  208.      -- the apropriate one.
  209.       local
  210.      i: INTEGER;
  211.       do
  212.      from  
  213.         i := lower1;
  214.      until
  215.         i > upper1
  216.      loop
  217.         Result := Result + storage.item(i).nb_occurrences(elt);
  218.         i := i + 1;
  219.      end;
  220.       ensure
  221.      Result >= 0;
  222.       end;
  223.       
  224.    fast_nb_occurrences(elt: G): INTEGER is
  225.      -- Number of occurrences using `='.
  226.       local
  227.      i: INTEGER;
  228.       do
  229.      from  
  230.         i := lower1;
  231.      until
  232.         i > upper1
  233.      loop
  234.         Result := Result + storage.item(i).fast_nb_occurrences(elt);
  235.         i := i + 1;
  236.      end;
  237.       ensure
  238.      Result >= 0;
  239.       end;
  240.  
  241. feature -- Resizing :
  242.  
  243.    resize(l1, u1, l2, u2: INTEGER) is
  244.       require
  245.      u1 >= l1 - 1;
  246.      u2 >= l2 - 1;
  247.       local
  248.      i: INTEGER;
  249.      oldl1, oldu1: INTEGER;
  250.      tmp: ARRAY[G];
  251.       do
  252.      oldl1 := lower1;
  253.      oldu1 := upper1;
  254.      storage.resize (l1,u1); 
  255.      from
  256.         i := l1;
  257.      until
  258.         i > u1
  259.      loop
  260.         if i < oldl1 or i > oldu1 then
  261.            !!tmp.make (lower2, upper2);
  262.            storage.put (tmp, i)
  263.         else
  264.            storage.item (i).resize (l2,u2);
  265.         end;
  266.         i := i + 1;
  267.      end;
  268.       ensure
  269.      lower1 = l1;
  270.      upper1 = u1;
  271.      lower2 = u2;
  272.      upper2 = u2;
  273.       end;
  274.  
  275.    resize1(l, u: INTEGER) is
  276.       require
  277.      u >= l - 1;
  278.       local
  279.      i, oldl, oldc: INTEGER;
  280.      tmp: ARRAY[G];
  281.       do
  282.      oldl := lower1;
  283.      oldc := upper1;
  284.      storage.resize (l,u);
  285.      if l < oldl then
  286.         from
  287.            i := l;
  288.         until
  289.            i = lower1
  290.         loop
  291.            !!tmp.make (lower2, upper2);
  292.            storage.put (tmp, i)
  293.            i := i + 1;
  294.         end;
  295.      end;
  296.      if u > oldc then
  297.         from
  298.            i := oldc + 1;
  299.         until
  300.            i > u
  301.         loop
  302.            !!tmp.make (lower2, upper2);
  303.            storage.put (tmp, i)
  304.            i := i + 1;
  305.         end;
  306.      end;
  307.       end;
  308.    
  309.    resize2 (l, u : INTEGER) is
  310.       require
  311.      u > l - 1;
  312.       local
  313.      i : INTEGER;
  314.       do
  315.      from
  316.         i := lower1;
  317.      until
  318.         i > upper1
  319.      loop
  320.         storage.item (i).resize (l,u);
  321.         i := i + 1;
  322.      end;
  323.       ensure
  324.      lower2 = l;
  325.      upper2 = u;
  326.       end;
  327.    
  328.  
  329.  
  330. feature {ANY} -- Other features :
  331.       
  332.    replace_all(x, r: G) is
  333.       local
  334.      i: INTEGER;
  335.       do
  336.      from 
  337.         i := lower1;
  338.      until
  339.         i > upper1
  340.      loop
  341.         storage.item(i).replace_all(x,r);
  342.         i := i + 1;
  343.      end;
  344.       end;
  345.    
  346.    fast_replace_all(x, r: G) is
  347.       local
  348.      i: INTEGER;
  349.       do
  350.      from 
  351.         i := lower1;
  352.      until
  353.         i > upper1
  354.      loop
  355.         storage.item(i).fast_replace_all(x,r);
  356.         i := i + 1;
  357.      end;
  358.       end;
  359.      
  360.    transpose is
  361.       local
  362.      i,j : INTEGER;
  363.      oldc1, oldc2 : INTEGER;
  364.       do
  365.      oldc1 := count1;
  366.      oldc2 := count2;
  367.      if count1 > count2 then
  368.         resize2 (lower2, lower2 + count1 -1);
  369.      elseif count2 > count1 then
  370.         resize1 (lower1, lower1 + count2 - 1);
  371.      end;
  372.      from  
  373.         i := lower1;
  374.      until 
  375.         i > upper1 - 1
  376.      loop
  377.         from  
  378.            j := i + 1;
  379.         until 
  380.            j > upper2
  381.         loop
  382.            swap(i,j,j,i);
  383.            j := j + 1;
  384.         end;
  385.         i := i + 1;
  386.      end;
  387.      resize(lower1,lower1 + oldc2 - 1,lower2,lower2 + oldc1 - 1);
  388.       ensure
  389.      count = oldc1 * oldc2;     
  390.       end;
  391.  
  392. invariant
  393.    
  394.    storage.count >= 1;
  395.    
  396. end -- ARRAY2
  397.